package com.wanhe.viewconf.views.child;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Handler;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.VideoView;

import com.orhanobut.logger.Logger;
import com.wanhe.base.Const;
import com.wanhe.base.model.disgn.Event;
import com.wanhe.base.model.disgn.ViewEvent;
import com.wanhe.base.model.disgn.ViewItem;
import com.wanhe.base.model.holder.FileHolder;
import com.wanhe.base.util.ObjectUtil;
import com.wanhe.viewconf.views.child.MealBookChildItemView;
import com.wanhe.viewconf.views.child.interfaces.IBindingViewFunc;
import com.wanhe.viewconf.views.child.interfaces.IViewReady;

/**
 * File Name:MealBookVideoView.java<br/>
 * Package Name:com.wanhe.viewconf.views.child.video<br/>
 * Date:2017/7/4 16:54<br/>
 * author:chendeji<br/>
 * desc: 菜单页面上的视频控件<br/>
 * Copyright (c) 2017/7/4, 781571323@qq.com All Rights Reserved.
 *
 */
public class MealBookVideoView extends MealBookChildItemView
		implements
			IViewReady ,
			IBindingViewFunc {

	public static final String TAG = "MealBookVideoView";

	private VideoView	videoView;
	private String		videoPath;

	public MealBookVideoView(Context context) {
		super(context);
	}

	private void initComponent() {
		videoView = new VideoView(getContext());
		videoView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT));
		videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
			@Override
			public void onPrepared(MediaPlayer mp) {
				mp.setLooping(true);
				mp.start();
			}
		});
		videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
			@Override
			public void onCompletion(MediaPlayer mp) {
				/**
				 * 防止部分视频不能循环播放的情况
				 * 
				 * @author 陈德基
				 * @time 2017/8/1 10:57
				 */
				mp.reset();
				showVideo();
			}
		});
		this.addView(videoView);
	}

	@Override
	public void initModelData(ViewItem item) {
		super.initModelData(item);
		int fid = item.getFile();
		getFilePath(fid);
	}

	private void getFilePath(int fid) {
		if (fid > 0) {
			String fileName = FileHolder.getInstance().get(fid).getFileName();
			videoPath = Const.getUrl(fileName, Const.videoBaseUrl);
		}
	}

	@Override
	protected void onWindowVisibilityChanged(int visibility) {
		super.onWindowVisibilityChanged(visibility);
		switch (visibility) {
			case VISIBLE :
				this.removeAllViews();
				setViewStatu(VIEW_DIRTY);
				// 修改BUG 退出到后台后视频不会再重新开始播放的问题
				initComponent();
				break;

			case GONE :
				if (videoView != null) {
					this.removeView(videoView);
					videoView = null;
				}
				break;
		}

	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		showVideo();
	}

	private void showVideo() {
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				if (videoView == null || videoView.isPlaying() || TextUtils.isEmpty(videoPath)) {
					videoView.setVisibility(INVISIBLE);
				} else {
					videoView.setVideoPath(videoPath);
				}
			}
		}, 250);
	}

	@Override
	public void show(Event event, ViewEvent.Coor coor) {
		super.show(event, coor);
		if (ObjectUtil.checkObject(event) && ObjectUtil.checkObject(event.getArgs())
				&& event.getArgs().getFile() > 0) {

			Event.ArgsBean args = event.getArgs();
			getFilePath(args.getFile());
		}

		removeAllViews();
		setViewStatu(VIEW_DIRTY);
		initComponent();
		showVideo();
	}

	@Override
	public void hide(Event event, ViewEvent.Coor coor) {
		super.hide(event, coor);
		setViewStatu(VIEW_DIRTY);
		removeAllViews();
	}

	@Override
	public void pause(Event event, ViewEvent.Coor coor) {
		super.pause(event, coor);
		/**
		 * 需要回收视频资源暂用的内存
		 * 
		 * @author 陈德基
		 * @time 2017/7/26 10:43
		 */
		if (videoView != null && videoView.isPlaying()) {
			videoView.pause();
		}
	}

	@Override
	public void resume(Event event, ViewEvent.Coor coor) {
		super.resume(event, coor);
		if (videoView != null && !videoView.isPlaying()) {
			videoView.resume();
		}
	}

	@Override
	public void ready(Event event, ViewEvent.Coor coor) {
		sendEvent(event);
	}

	@Override
	public void bind(Event event, ViewEvent.Coor coor) {
		if (ObjectUtil.checkObject(event)){
			String ctype = event.getCtype();
			if (!TextUtils.isEmpty(ctype)){
				switch (ctype){
					case ViewItem.VIEW_TYPE_ANIMATE:
						bindAnimateObj(event);
						break;
				}
			}
		}
	}
}
